package com.example.dw.controller;

import com.example.dw.common.Result;
import com.example.dw.dto.CertificateQueryDTO;
import com.example.dw.dto.NewsQueryDTO;
import com.example.dw.entity.*;
import com.example.dw.service.HomeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 首页控制器
 * @author system
 * @since 2024-01-01
 */
@Slf4j
@RestController
@RequestMapping("/home")
@Tag(name = "首页管理", description = "首页相关的API接口")
public class HomeController {

    @Autowired
    private HomeService homeService;

    /**
     * 获取热门课程
     */
    @GetMapping("/getCourseByHot")
    @Operation(summary = "获取热门课程", description = "获取热门课程列表")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "服务器错误")
    })
    public Result<List<Course>> getCourseByHot(@RequestParam(defaultValue = "10") Integer limit) {
        try {
            List<Course> courses = homeService.getCourseByHot(limit);
            return Result.success(courses);
        } catch (Exception e) {
            log.error("获取热门课程失败：{}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取精品课程
     */
    @GetMapping("/getCourseByGood")
    @Operation(summary = "获取精品课程", description = "获取精品推荐课程列表")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "服务器错误")
    })
    public Result<List<Course>> getCourseByGood(
            @Parameter(description = "限制数量", example = "10") @RequestParam(defaultValue = "10") Integer limit) {
        try {
            List<Course> courses = homeService.getCourseByGood(limit);
            return Result.success(courses);
        } catch (Exception e) {
            log.error("获取精品课程失败：{}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取最新活动
     */
    @GetMapping("/getActiveByNew")
    @Operation(summary = "获取最新活动", description = "获取最新活动列表")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "服务器错误")
    })
    public Result<List<Activity>> getActiveByNew(
            @Parameter(description = "限制数量", example = "10") @RequestParam(defaultValue = "10") Integer limit) {
        try {
            log.info("开始获取最新活动，limit: {}", limit);
            List<Activity> activities = homeService.getActiveByNew(limit);
            log.info("成功获取 {} 条最新活动", activities != null ? activities.size() : 0);
            return Result.success(activities);
        } catch (Exception e) {
            log.error("获取最新活动失败", e);
            String errorMessage = e.getMessage() != null ? e.getMessage() : e.getClass().getSimpleName();
            return Result.error("获取最新活动失败: " + errorMessage);
        }
    }

    /**
     * 获取最新新闻
     */
    @GetMapping("/getNewsByNew")
    @Operation(summary = "获取最新新闻", description = "获取最新发布的新闻列表，移动端使用")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "服务器错误")
    })
    public Result<List<News>> getNewsByNew(
            @Parameter(description = "限制数量", example = "10") @RequestParam(defaultValue = "10") Integer limit) {
        try {
            List<News> newsList = homeService.getNewsByNew(limit);
            return Result.success(newsList);
        } catch (Exception e) {
            log.error("获取最新新闻失败：{}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取热门课程Web版（分页）
     */
    @GetMapping("/getCourseByHotWeb")
    @Operation(summary = "获取热门课程Web版", description = "分页获取热门课程列表，用于Web端展示")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "服务器错误")
    })
    public Result<Map<String, Object>> getCourseByHotWeb(
            @Parameter(description = "页码，从1开始", example = "1") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小", example = "10") @RequestParam(defaultValue = "10") Integer size) {
        try {
            Map<String, Object> result = homeService.getCourseByHotWeb(page, size);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取热门课程Web版失败：{}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取精品课程Web版（分页）
     */
    @GetMapping("/getCourseByGoodWeb")
    @Operation(summary = "获取精品课程Web版", description = "分页获取精品推荐课程列表，用于Web端展示")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "服务器错误")
    })
    public Result<Map<String, Object>> getCourseByGoodWeb(
            @Parameter(description = "页码，从1开始", example = "1") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小", example = "10") @RequestParam(defaultValue = "10") Integer size) {
        try {
            Map<String, Object> result = homeService.getCourseByGoodWeb(page, size);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取精品课程Web版失败：{}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取最新活动Web版（分页）
     */
    @GetMapping("/getActiveByNewWeb")
    @Operation(summary = "获取最新活动Web版", description = "分页获取最新活动列表，用于Web端展示")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "服务器错误")
    })
    public Result<Map<String, Object>> getActiveByNewWeb(
            @Parameter(description = "页码，从1开始", example = "1") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小", example = "10") @RequestParam(defaultValue = "10") Integer size) {
        try {
            Map<String, Object> result = homeService.getActiveByNewWeb(page, size);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取最新活动Web版失败：{}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取最新新闻Web版（分页）
     */
    @GetMapping("/getNewsByNewWeb")
    @Operation(summary = "获取最新新闻Web版", description = "分页获取最新新闻列表，用于Web端展示")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "服务器错误")
    })
    public Result<Map<String, Object>> getNewsByNewWeb(
            @Parameter(description = "页码，从1开始", example = "1") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小", example = "10") @RequestParam(defaultValue = "10") Integer size) {
        try {
            Map<String, Object> result = homeService.getNewsByNewWeb(page, size);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取最新新闻Web版失败：{}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 根据ID获取新闻
     */
    @PostMapping("/news")
    @Operation(summary = "根据ID获取新闻", description = "根据新闻ID获取单条新闻详情")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "400", description = "新闻ID不能为空"),
            @ApiResponse(responseCode = "500", description = "服务器错误")
    })
    public Result<News> getNewsById(
            @Parameter(description = "新闻查询条件，包含newsid") @RequestBody NewsQueryDTO queryDTO) {
        try {
            if (queryDTO.getNewsid() == null) {
                return Result.error("新闻ID不能为空");
            }
            News news = homeService.getNewsById(queryDTO.getNewsid());
            return Result.success(news);
        } catch (Exception e) {
            log.error("根据ID获取新闻失败：{}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取所有新闻统一接口（分页）
     */
    @PostMapping("/newsAllPage")
    @Operation(summary = "获取所有新闻统一接口", description = "根据查询条件分页获取新闻列表")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "服务器错误")
    })
    public Result<Map<String, Object>> getAllNewsUni(
            @Parameter(description = "新闻查询条件") @RequestBody NewsQueryDTO queryDTO) {
        try {
            Map<String, Object> result = homeService.getAllNewsUni(queryDTO);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取所有新闻统一接口失败：{}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取所有新闻Web版（分页）
     */
    @PostMapping("/newsall")
    @Operation(summary = "获取所有新闻Web版", description = "分页获取所有新闻列表，Web端使用")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "服务器错误")
    })
    public Result<Map<String, Object>> getAllNewsPage(
            @Parameter(description = "页码，从1开始", example = "1") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小", example = "10") @RequestParam(defaultValue = "10") Integer size) {
        try {
            Map<String, Object> result = homeService.getAllNewsPage(page, size);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取所有新闻Web版失败：{}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取跑马灯
     */
    @GetMapping("/runD")
    @Operation(summary = "获取跑马灯", description = "获取首页滚动跑马灯信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "服务器错误")
    })
    public Result<List<HomeRund>> getRunningText() {
        try {
            List<HomeRund> runningTexts = homeService.getRunningText();
            return Result.success(runningTexts);
        } catch (Exception e) {
            log.error("获取跑马灯失败：{}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取通知Web版
     */
    @GetMapping("/notify")
    @Operation(summary = "获取通知列表", description = "获取系统通知列表，Web端使用")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "服务器错误")
    })
    public Result<List<Notify>> getNotifyList() {
        try {
            List<Notify> notifies = homeService.getNotifyList();
            return Result.success(notifies);
        } catch (Exception e) {
            log.error("获取通知列表失败：{}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取镜像文章
     */
    @GetMapping("/getMirror")
    @Operation(summary = "获取镜像文章", description = "获取镜像文章内容")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "服务器错误")
    })
    public Result<String> getMirror() {
        try {
            String mirror = homeService.getMirror();
            return Result.success(mirror);
        } catch (Exception e) {
            log.error("获取镜像文章失败：{}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取镜像图片
     */
    @GetMapping("/image")
    @Operation(summary = "获取镜像图片", description = "获取镜像图片URL")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "服务器错误")
    })
    public Result<String> getMirrorImg() {
        try {
            String mirrorImg = homeService.getMirrorImg();
            return Result.success(mirrorImg);
        } catch (Exception e) {
            log.error("获取镜像图片失败：{}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 证书查询
     */
    @PostMapping("/photo")
    @Operation(summary = "证书查询", description = "根据条件查询用户证书信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "500", description = "服务器错误")
    })
    public Result<List<Certificate>> searchCertificate(
            @Parameter(description = "证书查询条件") @RequestBody CertificateQueryDTO queryDTO) {
        try {
            List<Certificate> certificates = homeService.searchCertificate(queryDTO);
            return Result.success(certificates);
        } catch (Exception e) {
            log.error("证书查询失败：{}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取公告
     */
    @GetMapping("/bulletin")
    @Operation(summary = "获取公告列表", description = "获取系统公告信息列表")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "服务器错误")
    })
    public Result<List<Bulletin>> getBulletinList() {
        try {
            List<Bulletin> bulletins = homeService.getBulletinList();
            return Result.success(bulletins);
        } catch (Exception e) {
            log.error("获取公告列表失败：{}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }
} 